/**
 * 
 */
package org.rado.chessbatard.board;

import org.rado.chessbastard.board.exceptions.InvalidBordSquareException;
import org.rado.chessbastard.board.exceptions.InvalidPieceActionException;
import org.rado.chessbastard.board.exceptions.PieceActionException;
import org.rado.chessbastard.board.objects.IBoardSquare;
import org.rado.chessbastard.board.objects.actions.IPieceAction;
import org.rado.chessbastard.board.objects.pieces.Bishop;
import org.rado.chessbastard.board.objects.pieces.IChessPiece;
import org.rado.chessbastard.board.objects.pieces.King;
import org.rado.chessbastard.board.objects.pieces.Knight;
import org.rado.chessbastard.board.objects.pieces.Pawn;
import org.rado.chessbastard.board.objects.pieces.PieceColor;
import org.rado.chessbastard.board.objects.pieces.Queen;
import org.rado.chessbastard.board.objects.pieces.Rook;

/**
 * @author rado
 *
 */
public class ChessBoard implements IChessBoard {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6563958435069668906L;
	private IChessPiece[][] bord = new IChessPiece[8][8];
	
	public IChessPiece[][] getBord() {
		return bord;
	}

	/**
	 * Constructs a new chess board and populates it with
	 * the 2 player pieces
	 */
	public ChessBoard(){
		//add  white pieces
		 bord[0][0]= new Rook(PieceColor.WHITE);
		 bord[0][1]= new Knight(PieceColor.WHITE);
		 bord[0][2]= new Bishop(PieceColor.WHITE);
		 bord[0][3]= new King(PieceColor.WHITE);
		 bord[0][4]= new Queen(PieceColor.WHITE);
		 bord[0][5]= new Bishop(PieceColor.WHITE);
		 bord[0][6]= new Knight(PieceColor.WHITE);
		 bord[0][7]= new Rook(PieceColor.WHITE);
		 
		 bord[1][0]= new Pawn(PieceColor.WHITE);
		 bord[1][1]= new Pawn(PieceColor.WHITE);
		 bord[1][2]= new Pawn(PieceColor.WHITE);
		 bord[1][3]= new Pawn(PieceColor.WHITE);
		 bord[1][4]= new Pawn(PieceColor.WHITE);
		 bord[1][5]= new Pawn(PieceColor.WHITE);
		 bord[1][6]= new Pawn(PieceColor.WHITE);
		 bord[1][7]= new Pawn(PieceColor.WHITE);
		 
		//add black pieces 
		 bord[7][0]= new Rook(PieceColor.BLACK);
		 bord[7][1]= new Knight(PieceColor.BLACK);
		 bord[7][2]= new Bishop(PieceColor.BLACK);
		 bord[7][3]= new Queen(PieceColor.BLACK);
		 bord[7][4]= new King(PieceColor.BLACK);
		 bord[7][5]= new Bishop(PieceColor.BLACK);
		 bord[7][6]= new Knight(PieceColor.BLACK);
		 bord[7][7]= new Rook(PieceColor.BLACK);
		 
		 bord[6][0]= new Pawn(PieceColor.BLACK);
		 bord[6][1]= new Pawn(PieceColor.BLACK);
		 bord[6][2]= new Pawn(PieceColor.BLACK);
		 bord[6][3]= new Pawn(PieceColor.BLACK);
		 bord[6][4]= new Pawn(PieceColor.BLACK);
		 bord[6][5]= new Pawn(PieceColor.BLACK);
		 bord[6][6]= new Pawn(PieceColor.BLACK);
		 bord[6][7]= new Pawn(PieceColor.BLACK);	 
	}

	@Override
	public IChessPiece getPieceAt(IBoardSquare location) throws InvalidBordSquareException {
		if(location instanceof IBoardSquare){
			int y=location.getColumn();
			int x=location.getRow();
			IChessPiece piceAt=this.bord[x][y];
			return piceAt;
		}
		throw new InvalidBordSquareException("Square location is invalid!");
	}

	@Override
	public void executeAction(IPieceAction action) throws InvalidPieceActionException,PieceActionException {
		if(!(action instanceof IPieceAction)){
			throw new InvalidPieceActionException("Invalid move attempted. " +
					"There is no piece at initial location!");
		}
		action.execute(this);
	}

}
